package chatclient.applets.challengees;

import chatclient.netio.ClientAttr;
import chatclient.netio.AttrList;
import chatclient.applets.Chat;
import java.io.ByteArrayOutputStream;

public class ChallengeeID
        implements Challengee {


    @Override
    public byte[] getResponse(Chat chat, String s)
            throws Exception {
        AttrList attrlist = new AttrList();
        String s1;
        if ((s1 = chat.getParameter("nick")) != null && s1.length() > 0) {
            attrlist.add(new ClientAttr(ClientAttr.NICKNAME, s1));
        }
        if ((s1 = chat.getParameter("digest")) != null && s1.length() > 0) {
            attrlist.add(new ClientAttr(ClientAttr.DIGEST, bytesFromHexString(s1)));
        }
        if ((s1 = chat.getParameter("digestTime")) != null && s1.length() > 0) {
            attrlist.add(new ClientAttr(ClientAttr.DIGEST_TIME, Integer.parseInt(s1)));
        }
        if ((s1 = chat.getParameter("digestSalt")) != null && s1.length() > 0) {
            attrlist.add(new ClientAttr(ClientAttr.DIGEST_SALT, s1));
        }
        ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
        attrlist.write(bytearrayoutputstream);
        return bytearrayoutputstream.toByteArray();
    }

    private static byte[] bytesFromHexString(String s) {
        if (s == null) {
            return null;
        }
        byte abyte0[] = new byte[s.length() / 2];
        for (int i = 0; i < s.length(); i += 2) {
            abyte0[i / 2] = (byte) Integer.parseInt(s.substring(i, i + 2), 16);
        }

        return abyte0;
    }
}
